package com.test.thread;

/**
 * join，说白了就是让本来异步执行的多线程变成同步执行的。
 * 
 * 
 * @author noahedu
 *
 */
public class JoinThread extends Thread {
	public static int n = 0;

	static synchronized void inc() {

		n++;
		System.out.println("当前活动线程和N分别为:" + Thread.currentThread().getName()
				+ "_" + n);
	}

	public void run() {
		for (int i = 0; i < 10; i++)
			try {
				inc();
				sleep(3); // 为了使运行结果更随机，延迟3毫秒

			} catch (Exception e) {
			}
	}

	public static void main(String[] args) throws Exception {

		Thread threads[] = new Thread[100];
		for (int i = 0; i < threads.length; i++)
			// 建立100个线程
			threads[i] = new JoinThread();
		for (int i = 0; i < threads.length; i++) {// 运行刚才建立的100个线程
			threads[i].start();
			System.out.println("线程" + i + "start了");

			// threads[i].join();//关键点，此句注释不注释两种结果！！
			// http://wandejun1012.iteye.com/blog/1216971
		}

		System.out.println("n=" + JoinThread.n);
	}

}